﻿using System;
using System.Collections.Generic;
using System.Text;
using WeChat.PlatForm.Entity;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace WeChat.PlatForm.DataAccess.Repository
{
    public class MemberRepository
    {

        /// <summary>
        /// 根据用户名得到会员详情
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public async Task<MemberEntity> GetMemberByName(string email)
        {
            using (MyDBContext context = new MyDBContext())
            {  
                //得到一个游离态的对象
                return await context.Members.Where(a => a.MAIL_ADDRESS == email).AsNoTracking().FirstOrDefaultAsync();
            }
        }

        /// <summary>
        /// 注册会员
        /// </summary>
        /// <param name="password"></param>
        /// <param name="email"></param>
        /// <param name="nick"></param>
        /// <returns></returns>
        public async Task<int> RegisterMember(string password, string email, string nick)
        {
            using (MyDBContext context = new MyDBContext())
            {
                MemberEntity entiy = new MemberEntity()
                {
                    CREATE_TIME = DateTime.Now,
                    IS_DEL = false,
                    MAIL_ADDRESS = email,
                    NAME = nick,
                    PASS_WORD = password
                };
                //得到一个游离态的对象
                context.Members.Add(entiy);
                await context.SaveChangesAsync();

                return entiy.ID;
            }
        }
    }
}
